<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/14
 * Time: 15:05
 */

namespace App\Http\Controllers\Web;


use App\Service\AdvertService;

class AdvertController extends BaseController
{
    public function index()
    {
        $params = $this->request->post();
        return AdvertService::getList($params,'order','ASC');
    }

    public function getAdvertById()
    {
        $advertId = $this->request->post('id');
        if(!$advertId){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        return AdvertService::getAdvertById($advertId);
    }

    public function add()
    {
        $params = $this->request->post();
        if(!isset($params['img_src']) || empty(trim($params['img_src']))){
            return Response()->json(['code'=>201,'msg'=>'请上传广告图片']);
        }
        if(!isset($params['appid']) || empty(trim($params['appid']))){
            return Response()->json(['code'=>201,'msg'=>'小程序的APPID不能为空']);
        }
        if(!isset($params['page_path']) || empty(trim($params['page_path']))){
            return Response()->json(['code'=>201,'msg'=>'小程序的跳转路径不能为空']);
        }
        $params['order'] = $this->request->post('order',0);
        return AdvertService::add($params);
    }

    public function edit()
    {
        $params = $this->request->post();
        if(!isset($params['id']) || empty(trim($params['id']))){
            return Response()->json(['code'=>201,'msg'=>'请求参数缺失']);
        }
        if(!isset($params['img_src']) || empty(trim($params['img_src']))){
            return Response()->json(['code'=>201,'msg'=>'请上传广告图片']);
        }
        if(!isset($params['appid']) || empty(trim($params['appid']))){
            return Response()->json(['code'=>201,'msg'=>'小程序的APPID不能为空']);
        }
        if(!isset($params['page_path']) || empty(trim($params['page_path']))){
            return Response()->json(['code'=>201,'msg'=>'小程序的跳转路径不能为空']);
        }
        $params['order'] = $this->request->post('order',0);
        return AdvertService::edit($params);
    }

    public function destroy()
    {
        $ids = $this->request->post('ids');
        $ids = is_array($ids) ? $ids : [$ids];
        if(!$ids){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的广告']);
        }
        return AdvertService::destroy($ids);
    }

}
